﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Windows.Forms;
public partial class System_GraphControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public HZ.MBSM.BLL.GraphDataSet.GraphTableDataTable m_GraphTable;
    public HZ.MBSM.BLL.GraphDataSet.GraphTableDataTable GraphTable
    {
        get { return m_GraphTable; }
        set
        {
            m_GraphTable = value;
            Bitmap m_bmp = new Bitmap(800, 400);
            Graphics m_gp = Graphics.FromImage(m_bmp);
            SolidBrush redbrush = new SolidBrush(Color.Red);
            SolidBrush yellowbrush = new SolidBrush(Color.Yellow);
            SolidBrush bluebrush = new SolidBrush(Color.Blue);
            SolidBrush whitebrush = new SolidBrush(Color.White);
            Color[] ArrayColor ={ Color.Blue, Color.Red, Color.Yellow, Color.Violet, Color.YellowGreen, Color.PaleGreen, Color.Salmon, Color.SeaGreen, Color.Sienna, Color.OrangeRed, Color.Orange, Color.MediumSeaGreen };

            Pen blackpen = new Pen(Color.Black, 2);

            //创建一个矩形

            m_gp.FillRectangle(whitebrush, 0, 0, 800, 400);
            //划X轴

            m_gp.DrawLine(blackpen, new Point(0, 375), new Point(795, 375));
            //划Y轴

            m_gp.DrawLine(blackpen, new Point(5, 5), new Point(5, 400));
            //<!--得到最大的到货金额
            decimal _decMax = 0;
            decimal _decTemp = 0;
            for (int i = 0; i <= this.GraphTable.Count - 1; i++)
            {
                _decTemp = SysFun.ToDec(this.GraphTable[i][2]);
                if (_decTemp > _decMax)
                {
                    _decMax = _decTemp;
                }
            }
            if (_decMax == 0)
            {
                _decMax = 1;
            }
            //得到最大的到货金额-->

            //<!--划柱状图
            Font font = new System.Drawing.Font("Arial", 9, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(10, 100, 50, 50), Color.Blue, Color.DarkRed, 1.2f);
            m_gp.DrawString("万元", font, brush, 0, 0);
            float _fltX = 20;
            float _fltY = 374;
            for (int i = 0; i <= GraphTable.Count - 1; i++)
            {
                _decTemp = SysFun.ToDec(this.GraphTable[i][3]);
                _fltX = 20 + i * 65;
                _fltY = 374 - (float)(_decTemp / _decMax) * 360;
                SolidBrush redbrush11 = new SolidBrush(ArrayColor[i]);
                m_gp.FillRectangle(redbrush11, _fltX, _fltY, 50, 374 - _fltY);//矩形
                m_gp.DrawString(SysFun.ToTrim(this.GraphTable[i][3]), font, brush, _fltX, _fltY - 12);//到货金额
                m_gp.DrawString(SysFun.ToTrim(this.GraphTable[i][1]), font, brush, _fltX, 376);//日期
            }
            //划柱状图-->

            //<!--将图片存储到服务器上然后再绑定到Image控件中

            Response.ContentType = "image/gif";
            System.IO.Stream output = Response.OutputStream;
            DateTime dateNow = DateTime.Now;
            string _strTempFileName = dateNow.Year.ToString() + dateNow.Month.ToString() + dateNow.Day.ToString() + dateNow.Hour.ToString() + dateNow.Minute.ToString() + dateNow.Second.ToString();
            _strTempFileName = SysFun.PasswordEncrypt(_strTempFileName);
            string _strFilename = _strTempFileName + ".gif";
            //得到当前决定路径
            string _strFilePath = Server.MapPath("GraphList");
            _strFilePath += "\\";
            m_bmp.Save(_strFilePath + _strFilename);
            string _strFilePathName = _strFilePath + _strFilename;
            Image1.ImageUrl = "../Supervise/GraphList/" + _strFilename;
            //将图片存储到服务器上然后再绑定到Image控件中-->


            //System.Threading.Thread.Sleep(1000);
            //System.IO.File.Delete(_strFilePathName);
            //System.IO.MemoryStream ms = new System.IO.MemoryStream();
            //m_bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            //Response.ClearContent();
            //Response.ContentType = "image/Gif";
            //Response.BinaryWrite(ms.ToArray());
            
            //Font font = new System.Drawing.Font("Arial", 9, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
            //System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(10, 100, 50, 50), Color.Blue, Color.DarkRed, 1.2f);

            //m_gp.FillRectangle(redbrush, 10, 174 - m_h1, 50, m_h1);
            //m_gp.DrawString("2009-9-9", font, brush, 10, 176);
            //m_gp.FillRectangle(yellowbrush, 70, 174 - m_h2, 50, m_h2);
            //System.Drawing.Drawing2D.LinearGradientBrush brush1 = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(70, 20, 50, 50), Color.Blue, Color.DarkRed, 1.2f);

            //m_gp.DrawString("2008-9-9", font, brush1, 70, 174 - m_h2);
            //m_gp.FillRectangle(bluebrush, 130, 174 - m_h3, 50, m_h3);
            //System.Drawing.Drawing2D.LinearGradientBrush brush2 = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(130, 20, 50, 50), Color.Blue, Color.DarkRed, 1.2f);

            //m_gp.DrawString("2007-9-9", font, brush2, 130, 174 - m_h3);


            //Response.ContentType = "image/gif";

            //m_bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
        }
    }
}
